package com.peter.ThreadDemo;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class App {
	public static void main(String[] args) throws InterruptedException, ExecutionException {

		Threads threadUtils = new Threads.ThreadBuilder().setThreadGroup("PAD").build();

		threadUtils.setOperator(new Threads.operateThread() {

			@Override
			public void operatorBefore(Thread t) {
				System.out.println(
						"Excutor Before-------------------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.");
			}

			@Override
			public void operatorAfter(Thread t) {
				System.out.println(
						"Excutor After-------------------------------------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.");
			}

		});

//		MyThread<String> run = new MyThread<String>("strs");

		Runnable run = new Runnable() {

			@Override
			public void run() {
				System.out.println("In The Center");

			}
		};

		threadUtils.excute(run);

//		ThreadLocal<Integer> loal = run.getThreadLocal();

	}
}

class MyThread<T> implements Runnable {

	private ThreadLocal<Integer> ThreadLocal = (java.lang.ThreadLocal<Integer>) new ThreadLocal<T>();

	public MyThread(T t) {
//		ThreadLocal.set(123);
	}

	@Override
	public void run() {
		ThreadLocal.set(123);
	}

	public synchronized final ThreadLocal<Integer> getThreadLocal() {
		return ThreadLocal;
	}

	public synchronized final void setThreadLocal(ThreadLocal<Integer> threadLocal) {
		ThreadLocal = threadLocal;
	}

}

class Threads {

	private operateThread operator;

	private ThreadGroup threadGroup;

	public Threads() {
	}

	public synchronized final ThreadGroup getThreadGroup() {
		return threadGroup;
	}

	public synchronized final void setThreadGroup(ThreadGroup threadGroup) {
		this.threadGroup = threadGroup;
	}

	public static class ThreadBuilder {
		private ThreadGroup Group;

		public ThreadBuilder setThreadGroup(String groupID) {
			Group = new ThreadGroup(groupID);
			return this;
		}

		public Threads build() {
			Threads threads = new Threads();
			if (Group != null) {
				threads.setThreadGroup(Group);
			}
			return threads;
		}
	}

	@SuppressWarnings("unchecked")
	public <T, K> FutureTask<K> excute(T task) {

		FutureTask<K> futureTask = null;

		checkThreadGroup();
		checkOperator();

		Thread thread = null;
		if (task instanceof Runnable) {

			thread = new Thread(threadGroup, (Runnable) task);

			operator.operatorBefore(thread);
			thread.start();
			operator.operatorAfter(thread);

		} else if (task instanceof Callable<?>) {

			futureTask = new FutureTask<K>((Callable<K>) task);
			thread = new Thread(threadGroup, futureTask);

			operator.operatorBefore(thread);
			thread.start();
			operator.operatorAfter(thread);

		}

		return futureTask;
	}

	public static interface operateThread {
		public void operatorBefore(Thread t);

		public void operatorAfter(Thread t);
	}

	private void checkOperator() {
		if (operator == null) {
			operator = new operateThread() {
				@Override
				public void operatorBefore(Thread t) {
				}

				@Override
				public void operatorAfter(Thread t) {
				}

			};
		}
	}

	private void checkThreadGroup() {
		if (threadGroup == null) {
			threadGroup = new ThreadGroup("defaultGroup");
		}
	}

	public void setOperator(operateThread operator) {
		this.operator = operator;
	}
}

class Test {
	public static class MyRunnable implements Runnable {

		private ThreadLocal<Integer> threadLocal = new ThreadLocal<>();

		@Override
		public void run() {
			threadLocal.set((int) (Math.random() * 100D));

			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {

			}
			System.out.println(threadLocal.get());
		}
	}

	public static void main(String[] args) {
		MyRunnable sharedRunnableInstance = new MyRunnable();
		Thread thread1 = new Thread(sharedRunnableInstance);
		Thread thread2 = new Thread(sharedRunnableInstance);
		thread1.start();
		thread2.start();
	}
}
